package com.niit.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class SchoolTop100Bean implements WritableComparable<SchoolTop100Bean> {
    private long ActiveDays;  //活跃天数
    private long StudentsNum;  //学生人数

    public SchoolTop100Bean(){

    }

    public SchoolTop100Bean(long ActiveDays, long StudentsNum) {
        this.StudentsNum = StudentsNum;
        this.ActiveDays = ActiveDays;
    }

    /**
     *对有参构造进行修改，提供一个set方法
     * 自己封装对象的set方法，用于对象属性赋值
     */
    public void set(long ActiveDays, long StudentsNum) {
        this.StudentsNum = StudentsNum;
        this.ActiveDays = ActiveDays;
    }

    //3、set和get方法

    public long getActiveDays() {
        return ActiveDays;
    }

    public void setActiveDays(long ActiveDays) {
        this.ActiveDays = ActiveDays;
    }

    public long getStudentsNum() {
        return StudentsNum;
    }

    public void setStudentsNum(long StudentsNum) {
        this.StudentsNum = StudentsNum;
    }


    //修改一下，返回的都是数据
    @Override
    public String toString() {
        return "学生总人数数："+ StudentsNum + "\t" +"活跃天数："+ ActiveDays;
    }

    /**
     * 序列化方法，可以控制把哪写字段序列化出去
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(StudentsNum);
        dataOutput.writeLong(ActiveDays);
    }

    /**
     *  反序列化方法
     *  todo 注意反序列化的顺序和序列化顺序一致
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.StudentsNum = dataInput.readLong();
        this.ActiveDays = dataInput.readLong();
    }

    @Override
    public int compareTo(SchoolTop100Bean o) {
        return this.ActiveDays - o.getActiveDays() > 0 ? -1 : (this.ActiveDays - o.getActiveDays() < 0 ? 1 : 0) ;
    }
}
